<?php require_once('Connections/webtintuc.php'); ?>
<?php
// Load the common classes
require_once('includes/common/KT_common.php');

// Load the tNG classes
require_once('includes/tng/tNG.inc.php');

// Make a transaction dispatcher instance
$tNGs = new tNG_dispatcher("");

// Make unified connection variable
$conn_webtintuc = new KT_connection($webtintuc, $database_webtintuc);

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

mysql_select_db($database_webtintuc, $webtintuc);
$query_tinmoinhan = "SELECT * FROM tin WHERE AnHien = 1 ORDER BY idTin DESC LIMIT 0,3";
$tinmoinhan = mysql_query($query_tinmoinhan, $webtintuc) or die(mysql_error());
$row_tinmoinhan = mysql_fetch_assoc($tinmoinhan);
$totalRows_tinmoinhan = mysql_num_rows($tinmoinhan);

mysql_select_db($database_webtintuc, $webtintuc);
$query_tab_tinmoinhan = "SELECT * FROM tin WHERE AnHien = 1 ORDER BY idTin DESC LIMIT 0,10";
$tab_tinmoinhan = mysql_query($query_tab_tinmoinhan, $webtintuc) or die(mysql_error());
$row_tab_tinmoinhan = mysql_fetch_assoc($tab_tinmoinhan);
$totalRows_tab_tinmoinhan = mysql_num_rows($tab_tinmoinhan);

mysql_select_db($database_webtintuc, $webtintuc);
$query_tab_tinnoibat = "SELECT * FROM tin WHERE TinNoiBat = 1 ORDER BY idTin DESC LIMIT 0,10";
$tab_tinnoibat = mysql_query($query_tab_tinnoibat, $webtintuc) or die(mysql_error());
$row_tab_tinnoibat = mysql_fetch_assoc($tab_tinnoibat);
$totalRows_tab_tinnoibat = mysql_num_rows($tab_tinnoibat);

mysql_select_db($database_webtintuc, $webtintuc);
$query_tab_tinxemnhieu = "SELECT * FROM tin WHERE AnHien = 1 ORDER BY SoLanXem DESC LIMIT 0,10";
$tab_tinxemnhieu = mysql_query($query_tab_tinxemnhieu, $webtintuc) or die(mysql_error());
$row_tab_tinxemnhieu = mysql_fetch_assoc($tab_tinxemnhieu);
$totalRows_tab_tinxemnhieu = mysql_num_rows($tab_tinxemnhieu);

mysql_select_db($database_webtintuc, $webtintuc);
$query_master1loaitin = "SELECT * FROM loaitin WHERE idLT in (SELECT idLT FROM tin) ORDER BY Ten";
$master1loaitin = mysql_query($query_master1loaitin, $webtintuc) or die(mysql_error());
$row_master1loaitin = mysql_fetch_assoc($master1loaitin);
$totalRows_master1loaitin = mysql_num_rows($master1loaitin);

mysql_select_db($database_webtintuc, $webtintuc);
$query_detail2tin = "SELECT * FROM tin WHERE idLT=123456789 ORDER BY TieuDe LIMIT 0,5";
$detail2tin = mysql_query($query_detail2tin, $webtintuc) or die(mysql_error());
$row_detail2tin = mysql_fetch_assoc($detail2tin);
$totalRows_detail2tin = mysql_num_rows($detail2tin);

mysql_select_db($database_webtintuc, $webtintuc);
$query_lienketwebsite = "SELECT * FROM lienket";
$lienketwebsite = mysql_query($query_lienketwebsite, $webtintuc) or die(mysql_error());
$row_lienketwebsite = mysql_fetch_assoc($lienketwebsite);
$totalRows_lienketwebsite = mysql_num_rows($lienketwebsite);

mysql_select_db($database_webtintuc, $webtintuc);
$query_theloai = "SELECT * FROM theloai";
$theloai = mysql_query($query_theloai, $webtintuc) or die(mysql_error());
$row_theloai = mysql_fetch_assoc($theloai);
$totalRows_theloai = mysql_num_rows($theloai);

mysql_select_db($database_webtintuc, $webtintuc);
$query_loaitin = "SELECT * FROM loaitin WHERE idTL = 123456789";
$loaitin = mysql_query($query_loaitin, $webtintuc) or die(mysql_error());
$row_loaitin = mysql_fetch_assoc($loaitin);
$totalRows_loaitin = mysql_num_rows($loaitin);

mysql_select_db($database_webtintuc, $webtintuc);
$query_tinxemnhieu = "SELECT * FROM tin WHERE AnHien = 1 ORDER BY SoLanXem DESC LIMIT 0,6";
$tinxemnhieu = mysql_query($query_tinxemnhieu, $webtintuc) or die(mysql_error());
$row_tinxemnhieu = mysql_fetch_assoc($tinxemnhieu);
$totalRows_tinxemnhieu = mysql_num_rows($tinxemnhieu);

$maxRows_quangcao = 2;
$pageNum_quangcao = 0;
if (isset($_GET['pageNum_quangcao'])) {
  $pageNum_quangcao = $_GET['pageNum_quangcao'];
}
$startRow_quangcao = $pageNum_quangcao * $maxRows_quangcao;

mysql_select_db($database_webtintuc, $webtintuc);
$query_quangcao = "SELECT * FROM quangcao";
$query_limit_quangcao = sprintf("%s LIMIT %d, %d", $query_quangcao, $startRow_quangcao, $maxRows_quangcao);
$quangcao = mysql_query($query_limit_quangcao, $webtintuc) or die(mysql_error());
$row_quangcao = mysql_fetch_assoc($quangcao);

if (isset($_GET['totalRows_quangcao'])) {
  $totalRows_quangcao = $_GET['totalRows_quangcao'];
} else {
  $all_quangcao = mysql_query($query_quangcao);
  $totalRows_quangcao = mysql_num_rows($all_quangcao);
}
$totalPages_quangcao = ceil($totalRows_quangcao/$maxRows_quangcao)-1;

$maxRows_tintrongloai = 5;
$pageNum_tintrongloai = 0;
if (isset($_GET['pageNum_tintrongloai'])) {
  $pageNum_tintrongloai = $_GET['pageNum_tintrongloai'];
}
$startRow_tintrongloai = $pageNum_tintrongloai * $maxRows_tintrongloai;

$colname_tintrongloai = "-1";
if (isset($_GET['idLT'])) {
  $colname_tintrongloai = $_GET['idLT'];
}
mysql_select_db($database_webtintuc, $webtintuc);
$query_tintrongloai = sprintf("SELECT * FROM tin WHERE idLT = %s ORDER BY idTin DESC", GetSQLValueString($colname_tintrongloai, "int"));
$query_limit_tintrongloai = sprintf("%s LIMIT %d, %d", $query_tintrongloai, $startRow_tintrongloai, $maxRows_tintrongloai);
$tintrongloai = mysql_query($query_limit_tintrongloai, $webtintuc) or die(mysql_error());
$row_tintrongloai = mysql_fetch_assoc($tintrongloai);

if (isset($_GET['totalRows_tintrongloai'])) {
  $totalRows_tintrongloai = $_GET['totalRows_tintrongloai'];
} else {
  $all_tintrongloai = mysql_query($query_tintrongloai);
  $totalRows_tintrongloai = mysql_num_rows($all_tintrongloai);
}
$totalPages_tintrongloai = ceil($totalRows_tintrongloai/$maxRows_tintrongloai)-1;

$colname_loaitindangxem = "-1";
if (isset($_GET['idLT'])) {
  $colname_loaitindangxem = $_GET['idLT'];
}
mysql_select_db($database_webtintuc, $webtintuc);
$query_loaitindangxem = sprintf("SELECT * FROM loaitin WHERE idLT = %s", GetSQLValueString($colname_loaitindangxem, "int"));
$loaitindangxem = mysql_query($query_loaitindangxem, $webtintuc) or die(mysql_error());
$row_loaitindangxem = mysql_fetch_assoc($loaitindangxem);
$totalRows_loaitindangxem = mysql_num_rows($loaitindangxem);

// Make a login transaction instance
$loginTransaction = new tNG_login($conn_webtintuc);
$tNGs->addTransaction($loginTransaction);
// Register triggers
$loginTransaction->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "nutdangnhap");
$loginTransaction->registerTrigger("END", "Trigger_Default_Redirect", 99, "{kt_login_redirect}");
// Add columns
$loginTransaction->addColumn("kt_login_user", "STRING_TYPE", "POST", "username");
$loginTransaction->addColumn("kt_login_password", "STRING_TYPE", "POST", "password");
$loginTransaction->addColumn("kt_login_rememberme", "CHECKBOX_1_0_TYPE", "POST", "nho", 0);
// End of login transaction instance

// Make a logout transaction instance
$logoutTransaction = new tNG_logoutTransaction($conn_webtintuc);
$tNGs->addTransaction($logoutTransaction);
// Register triggers
$logoutTransaction->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "thoat");
$logoutTransaction->registerTrigger("END", "Trigger_Default_Redirect", 99, "index.php");
// Add columns
// End of logout transaction instance

$queryString_tintrongloai = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_tintrongloai") == false && 
        stristr($param, "totalRows_tintrongloai") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_tintrongloai = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_tintrongloai = sprintf("&totalRows_tintrongloai=%d%s", $totalRows_tintrongloai, $queryString_tintrongloai);

// Execute all the registered transactions
$tNGs->executeTransactions();

// Get the transaction recordset
$rscustom = $tNGs->getRecordset("custom");
$row_rscustom = mysql_fetch_assoc($rscustom);
$totalRows_rscustom = mysql_num_rows($rscustom);

$TFM_LimitLinksEndCount = 5;
$TFM_temp = $pageNum_tintrongloai + 1;
$TFM_startLink = max(1,$TFM_temp - intval($TFM_LimitLinksEndCount/2));
$TFM_temp = $TFM_startLink + $TFM_LimitLinksEndCount - 1;
$TFM_endLink = min($TFM_temp, $totalPages_tintrongloai + 1);
if($TFM_endLink != $TFM_temp) $TFM_startLink = max(1,$TFM_endLink - $TFM_LimitLinksEndCount + 1);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tin tức online</title>

<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>

<script type="text/javascript" language="JavaScript1.2" src="um_menu.js"></script>

<script type="text/javascript" src="sdmenu/sdmenu.js"></script>
<link href="sdmenu/sdmenu.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
// <![CDATA[
	var myMenu;
	window.onload = function() {
		myMenu = new SDMenu("my_menu");
		myMenu.init();
	};
	// ]]>
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
</script>

<link href="c1.css" rel="stylesheet" type="text/css" />


<!--Tab-->
<link rel="stylesheet" type="text/css" href="scripts/tabJQuery/css/style.css" />
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="scripts/tabJQuery/js/organictabs.jquery.js"></script>
<script type='text/javascript'>
        $(function() {

            $("#example-two").organicTabs({
                "speed": 200
            });
    
        });
    </script>
<!--//Tab-->

<script type="text/javascript" src="toolTipHTML/boxover.js"></script>

<link href="toolTipHTML/lightbox.css" rel="stylesheet" type="text/css" />
<link href="includes/skins/mxkollection3.css" rel="stylesheet" type="text/css" media="all" />
<script src="includes/common/js/base.js" type="text/javascript"></script>
<script src="includes/common/js/utility.js" type="text/javascript"></script>
<script src="includes/skins/style.js" type="text/javascript"></script>
</head>

<body background="bg.png">
<div id="container">
  <div id="header"><img src="images/mainheader.png" alt="mainheader" width="760" height="145" /></div>
  <div id="menu">
    <script type="text/javascript" language="JavaScript1.2" src="menungang.js"></script>
  </div>
  <div id="main">
    <div id="leftmain">
<div style="float: left" id="my_menu" class="sdmenu">

  <?php do { ?>
    <div> 
      <span><?php echo $row_theloai['TenTL']; ?></span> 
      <?php
  if ($totalRows_theloai>0) {
    $nested_query_loaitin = str_replace("123456789", $row_theloai['idTL'], $query_loaitin);
    mysql_select_db($database_webtintuc);
    $loaitin = mysql_query($nested_query_loaitin, $webtintuc) or die(mysql_error());
    $row_loaitin = mysql_fetch_assoc($loaitin);
    $totalRows_loaitin = mysql_num_rows($loaitin);
    $nested_sw = false;
    if (isset($row_loaitin) && is_array($row_loaitin)) {
      do { //Nested repeat
?>
        <a href="tintrongloai.php?idLT=<?php echo $row_loaitin['idLT']; ?>"><?php echo $row_loaitin['Ten']; ?></a>
        <?php
      } while ($row_loaitin = mysql_fetch_assoc($loaitin)); //Nested move next
    }
  }
?>
</div>
    <?php } while ($row_theloai = mysql_fetch_assoc($theloai)); ?></div>
      <div id="dongho">
        <script type="text/javascript">
AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','150','height','150','src','clock12','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','clock12' ); //end AC code
        </script>
        <noscript>
        <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="150" height="150">
          <param name="movie" value="clock12.swf" />
          <param name="quality" value="high" />
          <embed src="clock12.swf" width="150" height="150" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash"></embed>
        </object>
        </noscript>
      </div>
      
      <div id="iconsupport">      	
      	<A href="ymsgr:sendim?hoangvutp92@yahoo.com&amp;m=Hello">
        <img src="http://opi.yahoo.com/online?u=hoangvutp92@yahoo.com&m=g&t=15=us" 
        alt="Nhắp vào đây để liên hệ với admin" width="150" border=0 />        </A>        </div>

      <div id="divTinXemNhieu">
        <p class="tieudebox">TIN XEM NHIỀU</p>
        
        <?php do { ?>
          <p><a title="header=[
		<?php echo $row_tinxemnhieu['TieuDe']; ?>
	] 
	body=[
	      <div align='justify'>
		<img width='100' align='left' src='
			<?php echo $row_tinxemnhieu['urlHinh']; ?>
		'>
		<?php echo $row_tinxemnhieu['Ngay']; ?> 
 		<?php echo $row_tinxemnhieu['TomTat']; ?>
	</div>]
	" href="chitiettin.php?idTin=<?php echo $row_tinxemnhieu['idTin']; ?>"> <?php echo $row_tinxemnhieu['TieuDe']; ?></a></p>
      <?php } while ($row_tinxemnhieu = mysql_fetch_assoc($tinxemnhieu)); ?></div>
    </div>
    <div id="rightmain">
      
      <div id="thanhvien"> 
        <?php
//Show If User Is Logged In (region1)
$isLoggedIn = new tNG_UserLoggedIn($conn_webtintuc);
//Grand Levels: Any
if ($isLoggedIn->Execute()) {
?>
          <p>Chao anh <?php echo $_SESSION['kt_HoTen']; ?></p>
          <p><a href="doithongtin.php">Đổi thông tin</a></p>
          <p><a href="quantri">Quản trị</a></p>
          <form id="form1" name="form1" method="post" action="">
            <label>
            <?php
	echo $tNGs->getErrorMsg();
?><input type="submit" name="thoat" id="thoat" value="Thoát" />
            </label>
                              </form>
          <p>&nbsp;</p>
          <?php 
// else Show If User Is Logged In (region1)
} else { ?>
          <form id="formdangnhap" name="formdangnhap" method="post" action="">
          Tên đăng nhập
            <input name="username" type="text" class="loginfield" id="username" />
          Mật khẩu
          <input name="password" type="password" class="loginfield" id="password" />
          <label for="nutdangnhap"></label>
          <input type="submit" name="nutdangnhap" id="nutdangnhap" value="Đăng nhập" />
          <br />
          <input type="checkbox" name="nho" id="nho" />
Ghi nhớ
<label for="nho"></label>
          <br />
          <a href="#">Quên mật khẩu</a><br />
          <a href="dangky.php"> Đăng ký thành viên</a>
        </form>
  <?php
}
//End Show If User Is Logged In (region1)
?>
      </div>
      <div id="lienketwebsite">
      <select name="jumpMenu" id="jumpMenu" onchange="MM_jumpMenu('parent',this,0)">
        <option value="#">Liên kết website</option>
        <?php
do {  
?>
        <option value="<?php echo $row_lienketwebsite['Url']?>"><?php echo $row_lienketwebsite['Ten']?></option>
        <?php
} while ($row_lienketwebsite = mysql_fetch_assoc($lienketwebsite));
  $rows = mysql_num_rows($lienketwebsite);
  if($rows > 0) {
      mysql_data_seek($lienketwebsite, 0);
	  $row_lienketwebsite = mysql_fetch_assoc($lienketwebsite);
  }
?>
        </select>
       </div>
      <div id="timkiem">
        <form id="formtimkiem" name="formtimkiem" method="get" action="kqtim.php">
          <input onClick="this.value=''" name="tim" type="text" id="tim" value="Tìm kiếm" />
          <input type="image" name="imageField2" id="imageField2" src="images/tim.gif" />
                                </form>
      </div>
       <? include "formBinhChon.php" ;?>
      <div id="quangcao">
        <?php do { ?>
            <a href="<?php echo $row_quangcao['Url']; ?>"><img src="quangcao/<?php echo $row_quangcao['urlHinh']; ?>" width="139" border="0" /></a>
          <?php } while ($row_quangcao = mysql_fetch_assoc($quangcao)); ?>
      </div>
    </div>
    <div id="thongbao">
      <div id="thongbao_left">Tin mới nhận</div>
      <div id="thongbao_right">
        <marquee id="cuon" behavior="scroll" scrollamount="2" scrolldelay="1" onMouseOver="this.stop()" onMouseOut="this.start()">
        <?php do { ?>
          <?php echo $row_tinmoinhan['TieuDe']; ?> -
          <?php } while ($row_tinmoinhan = mysql_fetch_assoc($tinmoinhan)); ?>
        </marquee>
      </div>
    </div>
    <div id="content">
      <p>Bạn đang xem loại tin <?php echo $row_loaitindangxem['Ten']; ?></p>
      <table width="455" border="0" id="tintheoloai">
        <?php do { ?>
          <tr>
            <td><p><a href="chitiettin.php?idTin=<?php echo $row_tintrongloai['idTin']; ?>"><?php echo $row_tintrongloai['TieuDe']; ?></a></p>
                <p><img src="<?php echo $row_tintrongloai['urlHinh']; ?>" width="100" height="100" align="left" /><?php echo $row_tintrongloai['TomTat']; ?></p></td>
          </tr>
          <?php } while ($row_tintrongloai = mysql_fetch_assoc($tintrongloai)); ?>
      </table>
      <p align="center">&nbsp;
        <?php
for ($i = $TFM_startLink; $i <= $TFM_endLink; $i++) {
  $TFM_LimitPageEndCount = $i -1;
  if($TFM_LimitPageEndCount != $pageNum_tintrongloai) {
    printf('<a href="'."%s?pageNum_tintrongloai=%d%s", $currentPage, $TFM_LimitPageEndCount, $queryString_tintrongloai.'">');
    echo "$i</a>";
  }else{
    echo "<strong>$i</strong>";
  }
if($i != $TFM_endLink) echo("- ");}
?>
      </p>
      <p>&nbsp;</p>
    </div>
  </div>
  <div id="footer">
    <div id="footer2">
      <p>&nbsp;</p>
    </div>
    <p>&nbsp;</p>
  </div>
</div>
</body>
</html>
<?php
mysql_free_result($tinmoinhan);

mysql_free_result($tab_tinmoinhan);

mysql_free_result($tab_tinnoibat);

mysql_free_result($tab_tinxemnhieu);

mysql_free_result($master1loaitin);

mysql_free_result($detail2tin);

mysql_free_result($lienketwebsite);

mysql_free_result($theloai);

mysql_free_result($loaitin);

mysql_free_result($tinxemnhieu);

mysql_free_result($quangcao);

mysql_free_result($tintrongloai);

mysql_free_result($loaitindangxem);
?>
